/**
 * @class   wosh::gui::FormBuildingManager
 * @brief   Graphic interface of wosh::interfaces::services::BuildingManager bundle interface.
 *
 * Official implementation is wosh::services::BuildingManagerBundle
 *
 * @ingroup Gui
 ****************************************************************************
 * @version $Id: FormBuildingManager.h 2887 2010-08-11 08:09:41Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_Gui_FormBuildingManager_H__
 #define __WOSH_Gui_FormBuildingManager_H__

 #include <framework/gui/IWidget.h>
 #include <framework/gui/WoshTreeModel.h>
 #include <framework/gui/components/BusConnectorQt.h>
 #include <framework/gui/GuiUtilities.h>
 #include <QtGui/QMenu>
 #include <framework/building/BuildingObject.h>
 #include <interfaces/services/BuildingManager.h>
 

namespace Ui {
    class FormBuildingManager;
}
namespace wosh {
 namespace gui {


class WoshBuidlingObjectTreeModel :  public WoshTreeModel
 {
	Q_OBJECT

	public:
		WoshBuidlingObjectTreeModel( Tree* tree_data, QObject *parent = 0)
			: WoshTreeModel(tree_data, parent) { }
		virtual ~WoshBuidlingObjectTreeModel() { }

	public:
		int columnCount( const QModelIndex &parent = QModelIndex() ) const {
			(void)parent; return 5;
		 }

		QVariant data( const QModelIndex &index, int role ) const {
			if ( !index.isValid() )	return QVariant();
			TreeNode* item = static_cast<TreeNode*>(index.internalPointer());
			if ( item == NULL || item->getContent() == NULL ) return QVariant();
			const wosh::building::BuildingObject* bObj = item->getContent()->as<wosh::building::BuildingObject>();
			if ( bObj == NULL ) return QVariant("NULL");
			if ( role == Qt::DisplayRole || role == Qt::EditRole ) { // cos completer!
				switch (index.column()) {
					case 0: return QVariant(bObj->getName().c_str());
					case 1: return QVariant(bObj->getInterfaces().getImplementedClass().name.c_str());
					case 2: return QVariant(bObj->getInterfaces().toString().c_str());
					case 3: return QVariant(bObj->getChildren().count());
					case 4: return QVariant(bObj->getPermission().toString().c_str());
					default:
						return QVariant();
				 }
			 }
			else if ( role == Qt::ToolTipRole ) {
				//return QVariant( bObj->name.c_str() );
			 }
			else if ( index.column() == 0 && role == Qt::DecorationRole ) {
				if ( !bObj->getInterfaces().isEmpty() )
					return QVariant( GuiUtilities::createIconFor(bObj->getInterfaces().getImplementedClass().getName()) );
				return QVariant( GuiUtilities::createIconFor( wosh::building::BuildingObject::className() ) );
			 }
			return QVariant();
		 }

		QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const {
			if (role != Qt::DisplayRole) return QVariant();
			if (orientation == Qt::Horizontal) {
				 switch(section) {
					case 0:		return QString("Name");
					case 1:		return QString("Type");
					case 2:		return QString("Interfaces");
					case 3:		return QString("Children#");
					case 4:		return QString("Permission");
					default:	return QVariant();
				  }
			 }
			return QVariant();
		 }

}; // class def


class FormBuildingManager : public IWidgetGeneric
 {
	Q_OBJECT

	WOSH_CLASSNAME(wosh::gui::FormBuildingManager)
	WOSH_CLASS_ABSTRACT(false)


	public:
		FormBuildingManager(QWidget *parent = 0);
		virtual ~FormBuildingManager();

	public:
		void busMessage( const Message& message, const Bus* source );

	public slots:
		void refreshAll();
		void refreshTree();

		void reDrawAll();

	protected slots:
		void treeViewBObj_ContextMenu( const QPoint& pos );
		void tableBObjContextMenuRequested( const QPoint& pos, const QModelIndex& index );

		void bObj_doubleClicked( const QModelIndex& index );

		void updateBObjFromTree( const TreeNode* node );

	protected:
		void changeEvent(QEvent *e);

	protected:
		BusConnectorQt BusBuilding;
		WoshBuidlingObjectTreeModel* treeModel;

	protected:
		void buildMenus();
		QAction* menuBObjTitle;
		QMenu* menuBObjTree;
		QMenu* menuBObjTable;


	private:
		Ui::FormBuildingManager *m_ui;


}; // class def

 }; // namespace gui
}; // namespace wosh


#endif // __WOSH_Gui_FormBuildingManager_H__
